/*
 * Copyright (c) 2015 Uwantsoft Ltd.
 * www.uwantsoft.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Uwantsoft ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with Uwantsoft.
 */

package com.uwantsoft.business.facade;

import com.uwantsoft.business.facade.dto.user.UserDto;
import com.uwantsoft.business.facade.dto.user.UserOverviewQueryCondition;
import com.uwantsoft.business.service.UserServiceImpl;
import com.uwantsoft.business.service.domain.user.User;
import com.uwantsoft.util.paging.DefaultPaginatedList;
import com.uwantsoft.util.paging.PagingListLoader;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Chuanbao
 */
@Component(value = "userFacade")
public class UserFacadeImpl implements UserDetailsService {
    @Resource(name = "userService")
    private UserServiceImpl userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.loadUserByUsername(username);
        if(user != null){
            return user.convert();
        }
        return new UserDto();
    }

    public void createOrUpdateUser(UserDto userDto) {
        User user = userService.obtainUser(userDto.getGuid());
        if(user == null){
            user = User.createUser(userDto);
            userService.createUser(user);
        }else{
            user.update(userDto.getUsername(),userDto.getPassword(),userDto.getName());
            userService.updateUser(user);
        }
    }

    public UserDto obtainUser(String guid){
        User user = userService.obtainUser(guid);
        if(user == null){
            return new UserDto();
        }
        return user.convert();
    }

    public void deleteUser(String guid){
        User user = userService.obtainUser(guid);
        if(user != null){
            user.deleteUser();
            userService.updateUser(user);
        }
    }

    public DefaultPaginatedList obtainUserByPage(final UserOverviewQueryCondition userOverviewQueryCondition){
        return new DefaultPaginatedList(userOverviewQueryCondition).loadData(new PagingListLoader<UserDto>() {
            @Override
            public List<UserDto> loadList() {
                List<UserDto> userDtoList = new ArrayList<UserDto>();

                List<User> userList = userService.obtainUsers(userOverviewQueryCondition);
                if (userList != null) {
                    for (User user : userList) {
                        userDtoList.add(user.convert());
                    }
                }
                return userDtoList;
            }

            @Override
            public int loadFullListSize() {
                return userService.obtainUsersAmount(userOverviewQueryCondition);
            }
        });
    }
}
